<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:TestInput.php,2010/05/24 10:32:28 
 * 
 **************************************************************************/
 
 
 
/**
 * @file TestInput.php
 * @author huqingping
 * @date 2010/05/24 10:32:28
 * @version 1.0 
 * @brief 
 *  
 **/

final class TestInput extends TestCase 
{
	function testParams()
	{
		$get = array(
			'_if'=>'HapN',
			'_of'=>'json',
			'_ie'=>'gbk',
			'_oe'=>'gbk',
			'key1'=>"测试",
			'key3'=>'测3',
		);
		$post = array(
			'_if'=>'HapN2',
			'_of'=>'json',
			'key2'=>"测试2",
			'key3'=>'测',
		);
		$ret = $this->fetch('/HapN/testInput',
			array('get'=>$get,'post'=>$post));
		$json = $ret->json();
		$this->assertEquals('HapN2',$json['data']['_if']);
		$this->assertEquals('GBK',$json['data']['_ie']);
		$this->assertEquals('json',$json['data']['_of']);
		$this->assertEquals('UTF-8',$json['data']['_oe']);
		$this->assertEquals(mb_convert_encoding('测试','utf-8','gbk'),$json['data']['key1']);
		$this->assertEquals(mb_convert_encoding('测试2','utf-8','gbk'),$json['data']['key2']);
		$this->assertEquals(mb_convert_encoding('测','utf-8','gbk'),$json['data']['key3']);
		$this->assertEquals('',$json['data']['novalue']);
		$this->assertEquals('default',$json['data']['defaultvalue']);
	}

	function testXBAIDUIE()
	{
		$ret = $this->fetch('/HapN/testInput',
			array('post'=>array('k'=>1),'header'=>array('X-BAIDU-IE: utf-8'))
		);
		$json = $ret->json();
		$this->assertEquals('UTF-8',$json['data']['_ie']);

		$ret = $this->fetch('/HapN/testInput',
			array('post'=>array('k'=>1),'header'=>array('X-BAIDU-IE: other'))
		);
		$json = $ret->json();
		$this->assertFalse('UTF-8' == $json['data']['_ie']);
	}

	function testCookie()
	{
		$cookie = array(
			'key1'=>'v1',
			'key2'=>'v2',
			'key3'=>'v3',
		);
		$ret = $this->fetch('/HapN/testInput',
			array('cookie'=>$cookie));
		$this->assertEquals('v1',$ret->cookies['key1']);
		$this->assertEquals('v2',$ret->cookies['key2']);
		$this->assertEquals('v3',$ret->cookies['key3']);
	}

	function testPut()
	{
		$get['key'] = 'v'; 
		$get['key1'] = 'v'; 
		$get['_if'] = 'json'; 
		$put = array('key1'=>'v1','key2'=>'v2','key3'=>'v3');
		$putFile = '/tmp/HapN_put';
		file_put_contents($putFile, json_encode($put));
		$input['get'] = $get;
		$input['put'] = $putFile;

		$ret = $this->fetch('/HapN/testInput',$input);
		$json = $ret->json();
		$this->assertEquals('json',$json['data']['_of']);
		$this->assertEquals('v',$json['data']['key']);
		$this->assertEquals('v1',$json['data']['key1']);
		$this->assertEquals('v2',$json['data']['key2']);
		$this->assertEquals('v3',$json['data']['key3']);
	}

	function testHeader()
	{
		$input = array(
			'get'=>array('_of'=>'json'),
			'header'=>
			array(
				'CLIENTIP: 2.1.2.1',
				'Custom: Custom'
				)
			);
		$ret = $this->fetch('/HapN/testInput',$input);
		$json = $ret->json();
		$this->assertEquals('2.1.2.1',$json['data']['_ip']);
		$this->assertEquals('2.1.2.1',$ret->headers['CLIENTIP']);
		$this->assertEquals('Custom',$ret->headers['CUSTOM']);
	}
}



/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
